package com.buka.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.buka.constants.SystemConstants;
import com.buka.domain.ResponseResult;
import com.buka.dto.AddArticleDto;
import com.buka.dto.ArticleListDto;
import com.buka.mapper.ArticleMapper;
import com.buka.pojo.Article;
import com.buka.pojo.ArticleTag;
import com.buka.pojo.Category;
import com.buka.service.ArticleService;
import com.buka.service.ArticleTagService;
import com.buka.service.CategoryService;
import com.buka.util.BeanCopyUtils;
import com.buka.util.RedisUtil;
import com.buka.vo.ArticleDetailVo;
import com.buka.vo.HotArticleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 26755
 * @description 针对表【article(文章表)】的数据库操作Service实现
 * @createDate 2024-03-20 15:46:46
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public ResponseResult hotArticleList() {
        LambdaQueryWrapper<Article> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        lambdaQueryWrapper.orderByDesc(Article::getViewCount);
        lambdaQueryWrapper.last("limit 0,10");
        List<Article> list = list(lambdaQueryWrapper);
        List<HotArticleVo> articleVos = BeanCopyUtils.copyBeanList(list, HotArticleVo.class);
        return ResponseResult.okResult(articleVos);
    }

    @Override
    public ResponseResult articleList(Integer pageNum, Integer pageSize, Long categoryId) {
        LambdaQueryWrapper<Article> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        lambdaQueryWrapper.eq(categoryId != null && categoryId != 0, Article::getCategoryId, categoryId);
        lambdaQueryWrapper.orderByDesc(Article::getIsTop);
        Page<Article> page = new Page<>(pageNum, pageSize);
        page(page, lambdaQueryWrapper);
        List<Article> records = page.getRecords();
        List<ArticleListDto> articleListDtos = BeanCopyUtils.copyBeanList(records, ArticleListDto.class);
        for (ArticleListDto articleListDto : articleListDtos) {
            Category byId = categoryService.getById(articleListDto.getCategoryId());
            articleListDto.setCategoryName(byId.getName());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("rows", articleListDtos);
        map.put("total", page.getTotal());
        return ResponseResult.okResult(map);
    }

    @Override
    public ResponseResult getArticleDetail(Long id) {
        Article byId = getById(id);
        //从redis中获取viewCount
        Long viewCount = Long.parseLong(redisUtil.hGet("article:viewCount", id.toString()).toString());
//        Long viewCount = redisCache.getCacheMapValue("article:viewCount", id.toString());
        byId.setViewCount(viewCount);
        ArticleDetailVo articleDetailVo = BeanCopyUtils.copyBean(byId, ArticleDetailVo.class);
        Category byId1 = categoryService.getById(articleDetailVo.getCategoryId());
        articleDetailVo.setCategoryName(byId1.getName());
        return ResponseResult.okResult(articleDetailVo);
    }

    @Override
    public ResponseResult updateViewCount(Long id) {
        //更新redis中对应 id的浏览量
        redisUtil.hIncr("article:viewCount", id.toString(), 1);
        return ResponseResult.okResult();
    }

    @Autowired
    private ArticleTagService articleTagService;

    @Override
    public ResponseResult add(AddArticleDto articleDto) {
        //添加 博客
        Article article = BeanCopyUtils.copyBean(articleDto, Article.class);
        save(article);
        List<ArticleTag> articleTags = articleDto.getTags().stream().map(tagId -> new ArticleTag(article.getId(), tagId)).collect(Collectors.toList());
        //添加 博客和标签的关联
        articleTagService.saveBatch(articleTags);
        return ResponseResult.okResult();

    }

    @Override
    public ResponseResult list(int pageNum, int pageSize, String title, String summary) {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        Page<Article> page = new Page<>(pageNum, pageSize);
        if (StringUtils.hasText(title)) {
            queryWrapper.like(Article::getTitle, title);
        }
        if (StringUtils.hasText(summary)) {
            queryWrapper.like(Article::getSummary, summary);
        }
        Page<Article> articlePage = page(page, queryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("rows", articlePage.getRecords());
        map.put("total", articlePage.getTotal());
        return ResponseResult.okResult(map);
    }

    @Override
    public ResponseResult getArticleById(int id) {
        return ResponseResult.okResult(getById(id));
    }

    @Override
    public ResponseResult updateArticleById(Article article) {
        updateById(article);
        return ResponseResult.okResult();
    }
}




